<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Box-Sizing 属性详解</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            color: #333;
            line-height: 1.6;
            padding: 20px;
            min-height: 100vh;
        }
        
        .container {
            max-width: 1000px;
            margin: 0 auto;
            padding: 20px;
        }
        
        header {
            text-align: center;
            margin-bottom: 40px;
            padding: 20px;
            background: white;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        h1 {
            color: #2c3e50;
            margin-bottom: 10px;
            font-size: 2.5rem;
        }
        
        .subtitle {
            color: #7f8c8d;
            font-size: 1.2rem;
            margin-bottom: 20px;
        }
        
        .explanation {
            background: white;
            padding: 25px;
            border-radius: 10px;
            margin-bottom: 30px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        .box-container {
            display: flex;
            flex-wrap: wrap;
            gap: 30px;
            margin-bottom: 40px;
            justify-content: center;
        }
        
        .box {
            width: 200px;
            height: 200px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            color: white;
            font-weight: bold;
            border-radius: 8px;
            position: relative;
            transition: transform 0.3s ease;
        }
        
        .box:hover {
            transform: translateY(-5px);
        }
        
        .box-content {
            text-align: center;
            z-index: 2;
        }
        
        .box-title {
            font-size: 1.2rem;
            margin-bottom: 10px;
        }
        
        .box-size {
            background: rgba(0, 0, 0, 0.4);
            padding: 5px 10px;
            border-radius: 20px;
            font-size: 0.9rem;
        }
        
        .box1 {
            background-color: rgb(118, 34, 52);
            box-sizing: content-box;
            width: 200px;
            height: 200px;
            padding: 0px;
            border-right: 20px solid rgb(255, 157, 0);
        }
        
        .box2 {
            background-color: rgb(34, 118, 87);
            box-sizing: border-box;
            width: 200px;
            height: 200px;
            padding: 0px;
            border-right: 20px solid rgb(255, 157, 0);
        }
        
        .box3 {
            background-color: rgb(34, 52, 118);
            box-sizing: border-box;
            width: 200px;
            height: 200px;
            padding: 0px;
            border-right: 0px solid rgb(255, 157, 0);
        }
        
        .dimension-label {
            position: absolute;
            background: rgba(255, 255, 255, 0.9);
            color: #333;
            padding: 2px 5px;
            border-radius: 3px;
            font-size: 12px;
            pointer-events: none;
        }
        
        .width-label {
            bottom: -25px;
            left: 50%;
            transform: translateX(-50%);
        }
        
        .height-label {
            right: -45px;
            top: 50%;
            transform: translateY(-50%) rotate(90deg);
        }
        
        .box-wrapper {
            position: relative;
            margin: 30px 0;
        }
        
        .box-name {
            text-align: center;
            margin-top: 35px;
            font-weight: bold;
            color: #2c3e50;
        }
        
        .box-details {
            background: white;
            padding: 15px;
            border-radius: 8px;
            margin-top: 10px;
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
        }
        
        .comparison {
            background: white;
            padding: 25px;
            border-radius: 10px;
            margin-top: 40px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        
        .highlight {
            color: #e74c3c;
            font-weight: bold;
        }
        
        .tip {
            background: #e8f4fc;
            border-left: 4px solid #3498db;
            padding: 15px;
            margin: 20px 0;
            border-radius: 0 8px 8px 0;
        }
        
        footer {
            text-align: center;
            margin-top: 40px;
            color: #7f8c8d;
            font-size: 0.9rem;
        }
        
        @media (max-width: 768px) {
            .box-container {
                flex-direction: column;
                align-items: center;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>Box-Sizing 属性详解</h1>
            <p class="subtitle">理解CSS盒模型的关键属性</p>
        </header>
        
        <div class="explanation">
            <h2>什么是 Box-Sizing？</h2>
            <p><code>box-sizing</code> 是CSS的一个重要属性，它决定了元素的总宽度和高度是如何计算的。</p>
            
            <p>在CSS中，每个元素都被表示为一个矩形框，这个框由内容(content)、内边距(padding)、边框(border)和外边距(margin)组成。<code>box-sizing</code> 属性控制着这个矩形框的尺寸计算方式。</p>
            
            <div class="tip">
                <p>💡 <strong>提示：</strong> 默认情况下，元素的 <code>box-sizing</code> 属性值为 <code>content-box</code>，这意味着宽度和高度只包括内容区域，不包括内边距和边框。</p>
            </div>
        </div>
        
        <h2>Box-Sizing 属性值演示</h2>
        
        <div class="box-container">
            <div class="box-wrapper">
                <div class="box box1">
                    <div class="box-content">
                        <div class="box-title">content-box</div>
                        <div class="box-size">200×200 + 边框</div>
                    </div>
                </div>
                <div class="dimension-label width-label">总宽度: 220px</div>
                <div class="dimension-label height-label">高度: 200px</div>
                <div class="box-name">Box 1: content-box</div>
            </div>
            
            <div class="box-wrapper">
                <div class="box box2">
                    <div class="box-content">
                        <div class="box-title">border-box</div>
                        <div class="box-size">180×200 + 边框</div>
                    </div>
                </div>
                <div class="dimension-label width-label">总宽度: 200px</div>
                <div class="dimension-label height-label">高度: 200px</div>
                <div class="box-name">Box 2: border-box</div>
            </div>
            
            <div class="box-wrapper">
                <div class="box box3">
                    <div class="box-content">
                        <div class="box-title">border-box</div>
                        <div class="box-size">200×200</div>
                    </div>
                </div>
                <div class="dimension-label width-label">总宽度: 200px</div>
                <div class="dimension-label height-label">高度: 200px</div>
                <div class="box-name">Box 3: border-box (无边框)</div>
            </div>
        </div>
        
        <div class="box-details">
            <h3>详细解释：</h3>
            <ul>
                <li><span class="highlight">Box 1 (content-box)</span>: 宽度设置为200px，但由于右侧有20px的边框，元素的总宽度变为220px。这是默认的盒模型计算方式。</li>
                <li><span class="highlight">Box 2 (border-box)</span>: 宽度设置为200px，包括内容和边框。因此内容区域宽度为180px，右侧边框为20px，总宽度保持200px。</li>
                <li><span class="highlight">Box 3 (border-box)</span>: 宽度设置为200px，没有边框，因此内容区域宽度为200px，总宽度也是200px。</li>
            </ul>
        </div>
        
        <div class="comparison">
            <h2>两种模式的对比</h2>
            <p><strong>content-box (默认值):</strong></p>
            <p>元素的宽度 = 内容宽度 + 内边距 + 边框</p>
            <p>当我们设置 width: 200px; padding: 10px; border: 5px solid; 时，元素的总宽度将是 200px + 20px + 10px = 230px</p>
            
            <p><strong>border-box:</strong></p>
            <p>元素的宽度 = 设置的宽度（已经包含内容、内边距和边框）</p>
            <p>当我们设置 width: 200px; padding: 10px; border: 5px solid; 时，内容区域的宽度将变为 200px - 20px - 10px = 170px，但元素的总宽度保持200px</p>
            
            <div class="tip">
                <p>💡 <strong>最佳实践：</strong> 许多开发者选择全局设置 <code>box-sizing: border-box</code>，因为它使布局更加直观和易于管理。通常会在CSS开头添加：</p>
                <pre>* {
    box-sizing: border-box;
}</pre>
            </div>
        </div>
        
        <footer>
            <p>© 2023 CSS盒模型教程 | 通过实践学习box-sizing属性</p>
        </footer>
    </div>
</body>
</html>